package com.gitee.sop.message.manager;

import com.gitee.sop.message.Sender;
import com.gitee.sop.message.bo.SenderBo;
import com.gitee.sop.message.email.Email163Sender;
import com.gitee.sop.message.email.EmailQQSender;
import com.gitee.sop.message.sms.RongTongDaSmsSender;
import com.gitee.sop.message.sms.YiMeiSmsSender;
import com.gitee.sop.message.util.ObjectUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @author: huwei
 * @date: 2020/6/10 17:42
 * @version: 1.0.0
 */
@Component
public class SenderManagerServiceImpl implements SenderManagerService {
    private Inner inner;

    {
        this.inner = new Inner();
        this.inner.addSender(new SenderBo(YiMeiSmsSender.class.getSimpleName(), "亿美短信", new YiMeiSmsSender(), SenderEnum.sms.type));
        this.inner.addSender(new SenderBo(RongTongDaSmsSender.class.getSimpleName(), "蓉通达", new RongTongDaSmsSender(), SenderEnum.sms.type));

        this.inner.addSender(new SenderBo(Email163Sender.class.getSimpleName(), "163邮箱", new Email163Sender(), SenderEnum.email.type));
        this.inner.addSender(new SenderBo(EmailQQSender.class.getSimpleName(), "QQ邮箱", new EmailQQSender(), SenderEnum.email.type));
    }

    @Override
    public List<Map<String, Object>> getAllSmsSenderInfo() {
        return this.inner.getAllSenderInfo(SenderEnum.sms.type);
    }

    @Override
    public List<Map<String, Object>> getAllEmailSenderInfo() {
        return this.inner.getAllSenderInfo(SenderEnum.email.type);
    }

    @Override
    public Sender getSender(String code) {
        return this.inner.getSender(code);
    }

    @Override
    public Map<String, Object> getParameterTemplate(String code) {
        return this.inner.getParameterTemplate(code);
    }


    @Override
    public boolean send(String senderCode, String receiver, String message, String type, Map<String, Object> param) {
        if (Objects.isNull(param)) {
            return false;
        }
        if (StringUtils.isNotBlank(senderCode)) {
            return commonSend(type, senderCode, receiver, message, param);
        }
        return false;
    }

    private boolean commonSend(String type, String senderCode, String receiver, String message, Map<String, Object> param) {
        //发送信息
        boolean result;
        try {
            result = this.getSender(senderCode).send(type, receiver, message, param);
        } catch (Exception e) {
            result = false;
        }
        return result;
    }


    private class Inner {
        Map<String, SenderBo> senderMap = new HashMap<>(6);

        void addSender(SenderBo smsSenderBo) {
            this.senderMap.put(smsSenderBo.getCode(), smsSenderBo);
        }

        List<Map<String, Object>> getAllSenderInfo(String type) {
            List<Map<String, Object>> mapList = new ArrayList<>(this.senderMap.size());
            this.senderMap.forEach((k, v) -> {
                if (type.equals(v.getType())) {
                    mapList.add(ObjectUtil.toMapEx(v, "sender"));
                }
            });
            return mapList;
        }

        Sender getSender(String code) {
            return this.senderMap.get(code).getSender();
        }


        public Map<String, Object> getParameterTemplate(String code) {
            return this.senderMap.get(code).getSender().senderParameterTemplate();
        }
    }

    public enum SenderEnum {
        normal("normal"),
        sms("phone"),
        email("email");

        private String type;

        SenderEnum(String type) {
            this.type = type;
        }

        public String getType() {
            return type;
        }
    }
}
